package com.sunwayworld.basemodule.rm.lsiequipmentcertificate.service.impl;

import com.sunwayworld.basemodule.basedata.sampletype.bean.LimsSampleCategoryBean;
import com.sunwayworld.basemodule.coredata.method.bean.LimsMethodCertificateUserBean;
import com.sunwayworld.basemodule.rm.lsiequipmentcertificate.bean.LimsEquipmentCertificateBean;
import com.sunwayworld.basemodule.rm.lsiequipmentcertificate.service.LimsEquipmentCertificateService;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.support.PersistableMetadataHelper;
import com.sunwayworld.module.item.file.bean.CoreFileBean;
import com.sunwayworld.module.item.file.service.CoreFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.rm.lsiequipmentcertificate.dao.LimsEquipmentCertificateUserDao;
import com.sunwayworld.basemodule.rm.lsiequipmentcertificate.bean.LimsEquipmentCertificateUserBean;
import com.sunwayworld.basemodule.rm.lsiequipmentcertificate.service.LimsEquipmentCertificateUserService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsEquipmentCertificateUserServiceImpl implements LimsEquipmentCertificateUserService {

    @Autowired
    private LimsEquipmentCertificateUserDao limsEquipmentCertificateUserDao;
    @Autowired
    @Lazy
    private LimsEquipmentCertificateService limsEquipmentCertificateService;
    @Autowired
    private CoreFileService coreFileService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsEquipmentCertificateUserDao getDao() {
        return limsEquipmentCertificateUserDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsEquipmentCertificateUserBean limsEquipmentCertificateUser = jsonWrapper.parseUnique(LimsEquipmentCertificateUserBean.class);
        limsEquipmentCertificateUser.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsEquipmentCertificateUser);
        return limsEquipmentCertificateUser.getId();
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.DELETE)
    public void delete(RestJsonWrapperBean jsonWrapper) {
        List<LimsEquipmentCertificateUserBean> userBeans = jsonWrapper.parse(LimsEquipmentCertificateUserBean.class);
        List<Long> personIds = userBeans.stream().map(LimsEquipmentCertificateUserBean::getId).collect(Collectors.toList());
        List<LimsEquipmentCertificateUserBean> userBeanList = getDao().selectListByIds(personIds);
        if (userBeanList.isEmpty()) return;
        List<LimsEquipmentCertificateBean> certificateBeans=limsEquipmentCertificateService.selectListByFilter(
                SearchFilter.instance().match("personId",personIds).filter(MatchPattern.OR));
        String tableName = PersistableMetadataHelper.getTableName(LimsEquipmentCertificateUserBean.class);

        List<CoreFileBean> coreFileBeans = new ArrayList<>();
        userBeanList.forEach(t ->{
            CoreFileBean coreFileBean = new CoreFileBean();
            coreFileBean.setTargetId(tableName.toUpperCase() + "$" + t.getId());
            coreFileBeans.add(coreFileBean);
        });
        getDao().deleteByIdList(personIds);
        limsEquipmentCertificateService.getDao().deleteBy(certificateBeans);
        coreFileService.getDao().deleteBy(coreFileBeans, "TARGETID"); //删除附件
    }

}
